<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Provide与inject</title>
    <script src='./vue.global.js'></script>
</head>
<body>
    <div id='app'>
        <!-- 给确定的子组件传入确定的数据 -->
        <todos :list="list"></todos>
    </div>

    <!-- provide给后代组件透传数据 后代组件通过injec取得这些数据 -->
    <script>
        const app = Vue.createApp({
            data(){
                return {
                    list:["喂狗","喂猫","铲屎"]
                }
            },

            // computed:{
            //     listLength(){
            //         return this.list.length
            //     }
            // },

            // 给子孙后代提供数据，后代组件谁爱要谁自己去inject
            // provide:{
            //     user:"铲屎官",
            //     // listLength:Vue.computed(()=>this.list.length)
            // },

            // 给子孙后代提供数据，后代组件谁爱要谁自己去inject
            provide(){
                return {
                    user:"铲屎官",
                    listLength: Vue.computed(()=>this.list.length)
                }
            },

        })


        app.component("todos",{
            // 声明接受来自父组件传入的props
            props:["list"],
            template:`
            <div>
                {{list}}
                <grandson></grandson>
            </div>
            `
        })

        app.component("grandson",{
            // 拿到祖代provide下来的数据
            inject:["user","listLength"],

            template:`
            <h4>劳资是孙子</h4>
            user:{{user}}
            <br>
            listLength:{{listLength}}
            `
        })

        app.mount('#app')
    </script>
</body>
</html>